gl out "${output}/forPaper"
gl outs "${output}/forPaper/Table_S4_S5_S6_S7_S8"
qui do "${code}/Data Preparation/dataprep"


gen postCat = (date > mdy(3,23,2020)) + (date > mdy(5,23,2020)) + (date > mdy(7,23,2020)) ///
	+ (date > mdy(9,23,2020)) + (date > mdy(12,23,2020)) + (date > mdy(3,23,2021)) if !mi(date)
label define postCat 1 "0-2 months" 2 "2-4 months" 3 "4-6 months" 4 "6-9 months" ///
	5 "9-12 months" 6 "12-15 months"
label values postCat postCat
replace postCat = 6 if postCat == 5 & ctry == 6
tab postCat if ctry == 9

tempfile start
save `start'
foreach index in "_nw" "_fw" "_icw" {
	gl indexgl nw
	if  "`index'" == "_icw" gl indexgl icw
	if  "`index'" == "_fw" gl indexgl fw
	if  "`index'" == "_nw" gl snum 8
	if  "`index'" == "_icw" gl snum 7
	if  "`index'" == "_fw" gl snum 6	
		if "`index'" == "_icw" loc title_suf "(ICW Index)"
	if "`index'" == "_fw" loc title_suf "(Factor Index)"
	use `start', clear

	label var postCat "time"
	eststo clear
	eststo bgd: areg ___depression`index'  i.postCat if ctrytext == "bangladesh", absorb(pid) vce(cluster hhid)
	gen s1 = e(sample)
	eststo col: areg ___depression`index'  i.postCat if ctrytext == "colombia", absorb(pid) vce(cluster hhid)
	gen s2 = e(sample)
	eststo keng: areg ___depression`index'  i.postCat if ctrytext == "kenyage", absorb(pid) vce(cluster hhid)
	gen s3 = e(sample)
	eststo keni: areg ___depression`index'  i.postCat if ctrytext == "kenyaipush", absorb(pid) vce(cluster hhid)
	gen s4 = e(sample)
	eststo kenk: areg ___depression`index'  i.postCat if ctrytext == "kenyaklps", absorb(pid) vce(cluster hhid)
	gen s5 = e(sample)
	eststo npl: areg ___depression`index'  i.postCat if ctrytext == "nepal", absorb(pid) vce(cluster hhid)
	gen s6 = e(sample)
	eststo nga: areg ___depression`index'  i.postCat if ctrytext == "nigeria", absorb(pid) vce(cluster hhid)
	gen s7 = e(sample)
	eststo rwa: areg ___depression`index'  i.postCat if ctrytext == "rwanda", absorb(pid) vce(cluster hhid)
	gen s8 = e(sample)
	eststo sl: areg ___depression`index'  i.postCat if ctrytext == "sierraleone", absorb(pid) vce(cluster hhid)
	gen s9 = e(sample)
	eststo drc: areg ___depression`index'  i.postCat if ctrytext == "drc", absorb(pid) vce(cluster hhid)
	gen s10 = e(sample)

	estadd scalar N_Clust = e(N_Clust), replace: *


	esttab *  using "${outs}/basic_${indexgl}.tex",  nobaselevels ///
						keep(*post**) booktabs replace label  wrap se star(* .1 ** .05 *** .01) ///
						order(0.postCat 1.postCat 2.postCat 3.postCat 4.postCat 5.postCat 6.postCat ) ///
						title(\label{tab:basic${indexgl}} Pre-post differences in depression index `title_suf')  ///
						mtitles("BGD" "COL" "KEN2" "KEN3" "KEN1" "NPL" "NGA" "RWA" "SLE" "DRC"  ) // 
	
	esttab *  using "${outs}/TableS${snum}.tex",  nobaselevels ///
						keep(*post**) booktabs replace label  wrap se star(* .1 ** .05 *** .01) ///
						order(0.postCat 1.postCat 2.postCat 3.postCat 4.postCat 5.postCat 6.postCat ) ///
						title(\label{tab:basic${indexgl}} Pre-post differences in depression index `title_suf')  ///
						mtitles("BGD" "COL" "KEN2" "KEN3" "KEN1" "NPL" "NGA" "RWA" "SLE" "DRC"  ) // 
							
	gen moy = month(date)
	recode moy (1 2 = 1 "Jan-Feb") (3 4 = 2 "Mar-April") (5 6 = 3 "May-June") ///
		(7 8 = 4 "July-Aug") (9 10 = 5 "Sept-Oct") (11 12 = 6 "Nov-Dec"), gen(mcat)
	gen year = year(date)


	foreach c in rwanda kenyaklps colombia{
	preserve
	keep if ctrytext == "`c'"
	eststo `c'1: areg ___depression`index' i.postCat, absorb(pid) vce(cluster hhid)
	*eststo: areg ___depression`index' i.postCat i.mcat_`c' , absorb(pid) vce(cluster hhid)
	eststo `c'2: areg ___depression`index' year i.postCat i.mcat , absorb(pid) vce(cluster hhid)
	*eststo: areg ___depression`index' year i.postCat i.moy , absorb(pid) vce(cluster hhid)
	*eststo: areg ___depression`index' date i.postCat if month0 > 17, absorb(pid) vce(cluster hhid)
	*eststo: areg ___depression`index' date i.postCat if month0 > 16, absorb(pid) vce(cluster hhid)
	*eststo: areg ___depression`index' date i.postCat if month0 > 12, absorb(pid) vce(cluster hhid)
	restore
	}

	keep if ctrytext == "kenyaipush"
	egen mdate = mean(date), by(postCat)
	replace date = mdate if postCat != 0
	replace year = date/365
	eststo keni_pt: areg ___depression`index'  i.postCat year, absorb(pid) vce(cluster month)

	qui do "${code}/Data Preparation/dataprep_kenya_ge"
	*keep if ctrytext == "kenyage"
	merge m:1 date using "${raw}/kenya_ge/kenya_ge_seas", nogen keep(1 3)

	gen postCat = (date > mdy(3,23,2020)) + (date > mdy(5,23,2020)) + (date > mdy(7,23,2020)) ///
		+ (date > mdy(9,23,2020)) + (date > mdy(12,23,2020)) + (date > mdy(3,23,2021)) if !mi(date)
	label define postCat 1 "0-2 months post" 2 "2-4 months post" 3 "4-6 months post" 4 "6-9 months post" ///
		5 "9-12 months post" 6 "12-15 months post"
	label values postCat postCat

	egen _mctrl_seas = mean(_ctrl_seas), by(month)
	gen __ctrl_seas = _ctrl_seas if date < mdy(3,1,2020)
	replace __ctrl_seas = _mctrl_seas if date > mdy(3,1,2020)
	label var __ctrl_seas "Seasonal Food Security"
	egen mdate = mean(date), by(month)
	keep if month != 52
	replace date = mdate if date > mdy(3,1,2020)
	gen year = year(date) + doy(date)/365
	eststo kenGE2: areg ___depression`index' i.postCat __ctrl_seas, absorb(pid) vce(cluster hhid)	
	gen moy = month(date)
	gen s = e(sample)
	eststo kenGE3: reg __ctrl_seas i.moy year if s	


	qui do "${code}/Data Preparation/dataprep_nepal"
	gen postCat = (date > mdy(3,23,2020)) + (date > mdy(5,23,2020)) + (date > mdy(7,23,2020)) ///
		+ (date > mdy(9,23,2020)) + (date > mdy(12,23,2020)) + (date > mdy(3,23,2021)) if !mi(date)
	label define postCat 1 "0-2 months" 2 "2-4 months" 3 "4-6 months" 4 "6-9 months" ///
		5 "9-12 months" 6 "12-15 months"
	label values postCat postCat

	*				0.1 Aggregate food security to analysis time periods
	*-------------------------------------------------------------------------------
	egen mseas = mean(_ctrl_seas), by(postCat)
	gen ctrl_seasAg = mseas
	replace ctrl_seasAg = _ctrl_seas if date < mdy(2, 1, 2020)
	egen _mctrl_seas = mean(_ctrl_seas), by(month)
	gen __ctrl_seas = _ctrl_seas if date < mdy(3,1,2020)
	replace __ctrl_seas = _mctrl_seas if date > mdy(3,1,2020)
	replace __ctrl_seas = -1*__ctrl_seas
	label var __ctrl_seas "Seasonal Food Security"
	egen mdate = mean(date), by(month)
	replace date = mdate if month > 7
	eststo npl2: areg ___depression`index' i.postCat __ctrl_seas, absorb(pid) vce(cluster hhid)	


	estadd scalar GDP_Growth = 3.0: kenyaklps2* keni_pt
	estadd scalar GDP_Growth = 1.9, replace: col*
	estadd scalar GDP_Growth = 6.6, replace: rwa*

	estadd scalar N_Clust = e(N_Clust), replace: *

	loc title_suf 
	if "`index'" == "_icw" loc title_suf "(ICW Index)"
	if "`index'" == "_fw" loc title_suf "(Factor Index)"
		if "`index'" == "_nw" loc tnum 1
		if "`index'" == "_icw" loc tnum S4
		if "`index'" == "_fw" loc tnum S5
		gl outuse ${outs}
		if "`index'" == "_nw" gl outuse ${out}
	
	
	esttab rwanda2 colombia2 kenyaklps2 kenGE2 npl2 keni_pt bgd  nga sl drc    using "${outuse}/best_estimates_${indexgl}_raw.tex", ///
						keep(*postCat*  year *ctrl*) booktabs replace label  wrap se star(* .1 ** .05 *** .01) ///
						title(Best Estimates from Each Sample `title_suf' \label{tab:best${indexgl}})  ///
						stats(N N_Clust, fmt(0 1) labels("Observations" "Households")) nobaselevels ///
						order(0.postCat 1.postCat 2.postCat 3.postCat 4.postCat 5.postCat 6.postCat ) ///
						mtitles("RWA" "COL" "KEN1" "KEN2" "NPL" "KEN3" "BGD"  "NGA" "SLE" "DRC"   ) ///
						mgroups("Season + Time Trend"  "Seasonal Food Security Ctrl" "Time Control" "Pre-Post Only" , ///
							pattern(1 0 0 1 0 1 1 0 0 0 ) ///
							prefix(\multicolumn{@span}{c}{) suffix(}) ///
							span erepeat(\cmidrule(lr){@span}))
							
	esttab rwanda2 colombia2 kenyaklps2 kenGE2 npl2 keni_pt bgd  nga sl drc    using "${outuse}/Table`tnum'.tex", ///
						keep(*postCat*  year *ctrl*) booktabs replace label  wrap se star(* .1 ** .05 *** .01) ///
						title(Best Estimates from Each Sample `title_suf' \label{tab:best${indexgl}})  ///
						stats(N N_Clust, fmt(0 1) labels("Observations" "Households")) nobaselevels ///
						order(0.postCat 1.postCat 2.postCat 3.postCat 4.postCat 5.postCat 6.postCat ) ///
						mtitles("RWA" "COL" "KEN1" "KEN2" "NPL" "KEN3" "BGD"  "NGA" "SLE" "DRC"   ) ///
						mgroups("Season + Time Trend"  "Seasonal Food Security Ctrl" "Time Control" "Pre-Post Only" , ///
							pattern(1 0 0 1 0 1 1 0 0 0 ) ///
							prefix(\multicolumn{@span}{c}{) suffix(}) ///
							span erepeat(\cmidrule(lr){@span}))

							
}


/*use `start', clear


foreach index in "_fw" "_nw" "_icw" {
			gl outuse ${outs}
		if "`index'" == "_nw" gl outuse ${out}
filefilter "${outuse}/best_estimates`index'_raw.tex" "${outuse}/best_estimates`index'.tex" , replace ///
	from("end{tabular}") to("end{tabular}\r \BScaption\42h\123h\BSfootnotesize Note Dependent")
	
}
	variable is validated depression measure when available and unweighted depression index when unavailable. All measures are positively coded so that positive indicates improved mental health. Inde[pendent variables of interest are indicator variables for discrete intervals of time post-COVID (0-2 months, 2-4 months, etc.). All models include individual fixed effects and all standard errors are clustered at the level of the household. Samples are ordered from left to right with our strongest estimation strategies on the left. The first three columns include controls for month of year and the date in yearly units. For post-COVID periods, the linear time variable is set to the average date within the discrete time period of interest. The fourth and fifth columns include controls for the average levels of seasonal hunger at the survey date in prior years. The sixth column includes a linear time control only. The remaining columns include no additional controls.\125h")
}
